Page({
    data: {
        username: '',
        password: ''
    },
    getUsername(e) {
        this.setData({ username: e.detail.value })
    },
    getPassoword(e) {
        this.setData({ password: e.detail.value })
    },

    login() {
        let username = this.data.username
        let password = this.data.password
        if (username.length < 6 || username.length > 14) {
            wx.showToast({
                icon: 'none',
                title: '账号必须大于6位且小于14位',
            })
            return
        }
        if (password.length < 6 || password.length > 14) {
            wx.showToast({
                icon: 'none',
                title: '密码必须大于6位且小于14位',
            })
            return
        }
        wx.request({
            url: 'http://120.77.156.205:8808/wx/ucenter/login',
            data: {
                username: username,
                password: password,
            },
            method: 'POST',
            header: {
                'content-type': 'application/json;charset=UTF-8'
            },
            success: (res) => {
                if (res.data.code == 0) {
                    wx.showToast({
                        icon: 'success',
                        title: '登陆成功'
                    })
                    wx.setStorageSync('username', this.data.username)
                    wx.setStorageSync('password', this.data.password)
                    wx.setStorageSync('userId', res.data.data.userMes.id)
                    wx.setStorageSync('status', res.data.data.userMes.status)
                    wx.setStorageSync('userMes', res.data.data.userMes)

                    //   wx.setStorageSync('key', data)

                    // 获取用户信息后，获取用户所有的鱼缸id
                    // console.log("从接口中获取所有鱼缸id")

                    wx.request({
                        url: `http://120.77.156.205:8808/wx/aquarium/aquariumIdList/${this.data.username}`,
                        method: 'GET',
                        success: (res) => {

                            console.log(res)
                            if (res.data.code === 6) {
                                console.log("该用户没有添加水族箱！")

                                wx.reLaunch({
                                    url: '../../pages/index/index',
                                })
                                return
                            }
                            if (res.data.code === 0) {
                                //本地存储该用户所有鱼缸id
                                wx.setStorageSync('userAquariumList', res.data.data.userAquariumList)
                                //表明用户有添加鱼缸
                                wx.setStorageSync('exists', "1")

                                //存储当前用户操作的鱼缸，先把索引为0的设为当前操作鱼缸
                                // wx.setStorageSync('curAquarium', res.data.data.userAquariumList[0])
                                //存储当前操作鱼缸的索引
                                wx.setStorageSync('curIndex', Number(0))
                                
                                var userAquariumArray = []
                                // res.data.data.userAquariumList.forEach(function(item,index){
                                //     let temp = {name:item,curIndex:index}
                                //     userAquariumArray.push(temp)
                                // })

                                for (var index in res.data.data.userAquariumList) {
                                    var temp = { name: res.data.data.userAquariumList[index], curIndex: Number(index) }

                                    userAquariumArray.push(temp)
                                }


                                wx.setStorageSync('userAquariumArray', userAquariumArray)

                                wx.reLaunch({
                                    url: '../../pages/index/index',
                                })
                            }

                        },

                    },

                    )

                }
                if (res.data.code == 1) {
                    wx.showToast({
                        icon: 'error',
                        title: '登陆失败'
                    })
                }
                if (res.data.code == 2) {
                    wx.showToast({
                        icon: 'error',
                        title: '资源找不到'
                    })
                }
                if (res.data.code == 3) {
                    wx.showToast({
                        icon: 'error',
                        title: '用户未注册'
                    })
                }
                if (res.data.code == 4) {
                    wx.showToast({
                        icon: 'error',
                        title: '帐号密码错误'
                    })
                }
                if (res.data.code == 5) {
                    wx.showToast({
                        icon: 'error',
                        title: '该账号已被注册'
                    })
                }

            },
            fail: (err) => {
                wx.showToast({
                    title: '服务器异常',
                    icon: "error"
                })
            }
        })
    },

})